package cn.talkimages.web.admin.service.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.talkimages.framework.beans.PageArrayList;
import cn.talkimages.framework.beans.PageList;
import cn.talkimages.framework.utils.page.PageUtil;
import cn.talkimages.web.admin.dao.KeywordDAO;
import cn.talkimages.web.admin.service.KeywordService;
import cn.talkimages.web.beans.KeywordBean;
import cn.talkimages.web.service.impl.BaseServiceImpl;

public class KeywordServiceImpl extends BaseServiceImpl implements
		KeywordService {

	private KeywordDAO keywordDAO;

	// ----------------------- Select ----------------------- //

	/**
	 * 根据关键字名,取得关键字信息
	 * 
	 * @param name
	 * @return List<KeywordBean>
	 * @throws Exception
	 */
	public PageList<KeywordBean> getKeywordInfosByName(String name,
			Integer pageNo, Integer pageSize) throws Exception {

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("name", name);// 检索关键字名称

		Long total = keywordDAO.selectKeywordInfosCount(params);

		PageArrayList<KeywordBean> keywordPageList;

		// 有比配Keyword数据
		if (total > 0) {

			params.put("offset", PageUtil.getOffset(pageNo, pageSize));
			params.put("size", pageSize);

			// 取得Keyword信息
			List<KeywordBean> keywords = keywordDAO.selectKeywordInfos(params);

			keywordPageList = new PageArrayList<KeywordBean>(keywords);
		} else {
			keywordPageList = new PageArrayList<KeywordBean>();
		}

		keywordPageList.setNumFound(total);
		keywordPageList.setPageNo(pageNo);
		keywordPageList.setPageSize(pageSize);

		return keywordPageList;
	}

	/**
	 * 根据Keyword编号取得详细信息
	 * 
	 * @param id
	 * @return KeywordBean
	 * @throws Exception
	 */
	public KeywordBean getKeywordById(Long id) throws Exception {
		return keywordDAO.selectKeywordById(id);
	}

	// ----------------------- Update ----------------------- //

	/**
	 * 创建关键字
	 * 
	 * @param keyword
	 * @throws Exception
	 */
	public void insertKeyword(KeywordBean keyword) throws Exception {
		keywordDAO.insertKeyword(keyword);
	}

	/**
	 * 根据ID,更新关键字
	 * 
	 * @param keyword
	 * @throws SQLException
	 */
	public void updateKeywordById(KeywordBean keyword) throws Exception {
		keywordDAO.updateKeywordById(keyword);
	}

	// -------------------------- Getter & Setter -------------------------- //

	public KeywordDAO getKeywordDAO() {
		return keywordDAO;
	}

	public void setKeywordDAO(KeywordDAO keywordDAO) {
		this.keywordDAO = keywordDAO;
	}
}
